package com.wizz.trade.trading.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.trading.dto.FpositionDto;
import com.wizz.trade.trading.dto.TransactionDto;
import com.wizz.trade.trading.dto.page.ConsPageDto;
import com.wizz.trade.trading.dto.page.FpositionPageDto;
import com.wizz.trade.trading.dto.page.TransactionPageDto;
import com.wizz.trade.trading.entity.Transaction;
import com.wizz.trade.trading.service.FpositionService;
import com.wizz.trade.trading.service.TransactionService;
import com.wizz.trade.trading.vo.FpositionVo;
import com.wizz.trade.trading.vo.TransactionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 期货模块
 *
 * @author fyftless
 * @since 2023-08-28 02:16:03
 */
@RestController
@RequestMapping("/transaction")
public class TransactionController {

    @Resource
    private TransactionService transactionService;

    @Resource
    private FpositionService fpositionService;

    /**
     * 保值开仓
     * @param transactionDto 新增期货记录
     * @return response
     */
    @PostMapping("/transaction")
    @Transactional
    public ResponseResult<String> hedgeOpen(@RequestBody TransactionDto transactionDto){
        Integer transactionId = transactionService.addTransaction(transactionDto);
        FpositionDto fpositionDto = new FpositionDto();
        BeanUtils.copyProperties(transactionDto, fpositionDto);
        Transaction ts = transactionService.getById(transactionId);
        // TODO private Integer poshands;private BigDecimal fsprice;private String positionno;
        fpositionDto.setPoshands(12);
        fpositionDto.setFsprice(BigDecimal.valueOf(20));
        fpositionDto.setPositionno(ts.getPositionno());
        fpositionDto.setTransaction(transactionId);
        fpositionService.addFposition(fpositionDto);
        return new ResponseResult<>(StatusCode.OK, "操作成功");
    }

    /**
     * 分页查询期货记录
     * @param transactionPageDto 分页条件
     * @return response
     */
    @GetMapping("/transaction")
    public ResponseResult<PageVo<TransactionVo>> getTransactionPage(TransactionPageDto transactionPageDto){
        PageVo<TransactionVo> page = transactionService.getPage(transactionPageDto);
        return new ResponseResult<>(StatusCode.OK, "查询成功", page);
    }

    /**
     * 分页查询头寸记录
     * @param fpositionPageDto 分页条件
     * @return response
     */
    @GetMapping("/fposition")
    public ResponseResult<PageVo<FpositionVo>> getFpositionPage(FpositionPageDto fpositionPageDto){
        PageVo<FpositionVo> page = fpositionService.getPage(fpositionPageDto);
        return new ResponseResult<>(StatusCode.OK, "查询成功", page);
    }
}
